<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class ResClass extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_class';



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-01 10:45
     * @功能说明:修改并修改相关记录
     */
    public function updateConnect($dis,$data){

        $res = $this->dataUpdate($dis,$data);

        $where[] = ['store_id','=',$dis['store_id']];

        $where[] = ['date','>',time()];

        $where[] = ['class_id','=',$dis['id']];

        $class_date = new ClassDate();

        $list = $class_date->where($where)->select()->toArray();

        if(!empty($list)){

            foreach ($list as $v){

                $time_str = $class_date->changeTime($v['date'],$data['start_time'],$data['end_time']);

                $update = [

                    'class_title' => $data['title'],

                    'start_time'  => $time_str['start_time'],

                    'end_time'    => $time_str['end_time'],

                ];
                //修改排班记录表 只修改后面的
                $class_date->where($where)->update($update);
            }

        }

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 09:21
     * @功能说明:初始化休息班次
     */

    public function initreset($dis){

        $dis['type'] = 0;

        $data = $this->dataInfo($dis);

        if(empty($data)){

            $dis['title'] = '休息';

            $this->dataAdd($dis);
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-26 13:49
     * @功能说明:检查一下时间 不能有相同时间段
     */
    public function checkClass($input){

        $dis[] = ['uniacid','=',$input['uniacid']];

        $dis[] = ['store_id','=',$input['store_id']];

        if(!empty($input['id'])){

            $dis[] = ['id','<>',$input['id']];

        }

        $have = $this->where($dis)->where(['type'=>1])->select()->toArray();

        if(!empty($have)){

            $input['start_time'] = strtotime($input['start_time']);

            $input['end_time']   = strtotime($input['end_time']);
            //说明是次日的
            if($input['end_time']<=$input['start_time']){

                $input['end_time']+=86400;
            }

            $min_start = $input['start_time'];

            $max_end   = $input['end_time'];

            foreach ($have as $value){

                $start_time = strtotime($value['start_time']);

                $end_time   = strtotime($value['end_time']);
                //说明是次日的
                if($end_time<=$start_time){

                    $end_time+=86400;
                }
                //判断两个时间段是都有交集
                $status = $this->is_time_cross( $input['start_time'],$input['end_time'],$start_time,$end_time);

                if($status!=true){

                    return ['code'=>500,'msg'=>'已有该时间段'];
                }
                //获取最小的开始时间
                if($start_time<$min_start){

                    $min_start = $start_time;
                }
                //获取最大的开始时间
                if($end_time>$max_end){

                    $max_end = $end_time;

                }

            }

            if(($max_end-$min_start)>86400){

                return ['code'=>500,'msg'=>'总班次时间不得超过24小时'];

            }

        }

        return true;

    }

    /**
     * PHP计算两个时间段是否有交集（边界重叠不算）
     *
     * @param string $beginTime1 开始时间1
     * @param string $endTime1 结束时间1
     * @param string $beginTime2 开始时间2
     * @param string $endTime2 结束时间2
     * @return bool
     */
    public function is_time_cross($beginTime1 = '', $endTime1 = '', $beginTime2 = '', $endTime2 = '') {

        $status = $beginTime2 - $beginTime1;

        if ($status > 0) {

            $status2 = $beginTime2 - $endTime1;

            if ($status2 >= 0) {

                return true;

            } else {

                return false;
            }
        } else {

            $status2 = $endTime2 - $beginTime1;

            if ($status2 > 0) {

                return false;

            } else {

                return true;
            }

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-02-26 14:36
     * @功能说明:返回班次时间的表头
     */
    public function getDataTitle($day){

        $d = date('t',$day);

        $firstday = strtotime(date('Y-m-01', $day));

        for ($i = 1;$i<=$d;$i++){

            $date['day'] = $i;

            $iDay = $firstday+($i-1)*86400;

            $date['week'] = changeWeek(date('w',$iDay));

            $date['time'] = date('Y-m-d',$iDay);

            $date['time_str'] = $iDay;

            $data[] = $date;

        }

        return $data;

    }

















}